// preload.js

// const { contextBridge, ipcRenderer } = require("electron");
// window.ipcRenderer = ipcRenderer;
// contextBridge.exposeInMainWorld("electronApi", {
//   /**
//    * 获取设备mac地址
//    * @returns
//    */
//   getMacInfo: async () => {
//     return window.ipcRenderer.invoke("get-mac");
//   },
// });

const { contextBridge, ipcRenderer } = require("electron");
window.ipcRenderer = ipcRenderer;
const os = require("os");

contextBridge.exposeInMainWorld("electronAPI", {
  isElectron: true,
  getMAC: () => {
    const networkInterfaces = os.networkInterfaces();
    let macAddress, ipAddress;
    for (const iface of Object.values(networkInterfaces)) {
      for (const details of iface) {
        if (
          details.family === "IPv4" &&
          details.mac !== "00:00:00:00:00:00" &&
          details.address !== "127.0.0.1"
        ) {
          macAddress = details.mac;
          ipAddress = details.address;
        }
      }
    }
    return { macAddress, ipAddress };
  },
});

window.addEventListener("DOMContentLoaded", () => {
  console.log("页面初始化>>>→➡️➡️➡️");
  const replaceText = (selector, text) => {
    const element = document.getElementById(selector);
    if (element) element.innerText = text;
  };

  for (const type of ["chrome", "node", "electron"]) {
    replaceText(`${type}-version`, process.versions[type]);
  }
});
